package com.example.wxy2.controller;

import com.example.wxy2.entity.login;
import com.example.wxy2.service.TestService;

import com.example.wxy2.utils.Jwtutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("login")
public class loginController {


//    @Resource
//    private loginServiceImpl loginService;


    @GetMapping("qqq")
    public int aaa(){
        System.out.println(123234);
        return 123;
    }

//    @PostMapping("login")
//    public Result login(@RequestBody loginDto logindto){
//
////        String username = logindto.getUsername();
////        String password = logindto.getPwd();
////        if(StringUtils.isEmpty(username) ||StringUtils.isEmpty(password) ){
////            return false;
////        }
//
//        return loginService.login(logindto);
//    }

    @Autowired
    private TestService testService;

    //验证token
    @GetMapping("checkToken")
    @CrossOrigin
    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return Jwtutils.checkToken(token);
    }


    @PostMapping("addUser")
    @CrossOrigin
    public String adduser(@RequestBody login login){

        System.out.println(login);
       boolean b = checkadmin(login.getUsername());
       if (b){
           return "用户名已被注册";
       }
        testService.adduser(login);
       return "注册成功";

    }


    @PostMapping("findUser")
    @CrossOrigin
    public List<login> findUser(){
        List<login> users = testService.findUser();
        System.out.println(users);
        return users;
    }

    @PostMapping("deleteUser")
    @CrossOrigin
    public String deleteUser(@RequestBody login login){
        testService.deleteUser(login);
        return "删除成功";
    }

//    @GetMapping("checkadmin")
//    @CrossOrigin
//    public boolean checkadmin(@RequestParam("admin")String admin){
public boolean checkadmin(String admin){
        return testService.checkadmin(admin);
    }

    @PostMapping("login")
    @CrossOrigin
//    public Object login(@RequestParam(value = "username")String username,
//                        @RequestParam(value = "password")String password){
    public Object login(@RequestBody login login){
        Map<String, Object> map = new HashMap<>();
        if (login.getUsername() == null || login.getPassword() == null) {
            map.put("code", 100);
            map.put("msg", "请求参数不能为空");
        } else {
//            String user = testService.login(username, password);
            login user = testService.login(login.getUsername(), login.getPassword());
            if (user != null) {
                System.out.println(login.getUsername());
                System.out.println(login.getPassword());
                System.out.println(user.getUsername());
                if (login.getPassword().equals(user.getPassword())) {
                    String token = Jwtutils.createToken(user.getId());
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("token", token);
                    map.put("user", user.getUsername());
                } else {
                    map.put("code", 101);
                    map.put("msg", "用户名或密码不对");
                }
            } else {
                map.put("code", 102);
                map.put("msg", "不存在该用户");
            }

        }
        return map;
    }



}
